//===============================
//  功能 ：备注 
//  作者 ：上中野辅亚瑟王 
//  创建时间 ：2024-08-14  17-17-09 
//  Unity版本  ：2019.4.35f1 
//  变更时间 :  2024-08-14  17-17-09 
//===============================





using UnityEngine;
using UnityEngine.EventSystems;

public class MoveHandle : MonoBehaviour, IBeginDragHandler, IDragHandler,IEndDragHandler
{

    public event System.Action DragStart;
    public event System.Action DragEnd;

    private Vector2 _startPosition;
    private Vector2 _currentPosition;

    private float _minX;
    private float _maxX;

    private float _minY;
    private float _maxY;

    private RectTransform _rectTransform;

    private Vector3[] _corners = new Vector3[4];

    private Rect screenRect=new Rect(0,0,Screen.width,Screen.height);

    private bool canMove = false;


    private void Awake()
    {
        _rectTransform = GetComponent<RectTransform>();
    }

    public void OnBeginDrag(PointerEventData eventData)
    {

        _startPosition = _rectTransform.position;


        //限制移动范围
        {
            //获取四个顶点位置
            _rectTransform.GetWorldCorners(_corners);
            float minx = _corners[0].x;
            float miny = _corners[0].y;
            float maxx = _corners[0].x;
            float maxy = _corners[0].y;

            for (int i = 1; i < _corners.Length; i++)
            {
                minx = Mathf.Min(minx, _corners[i].x);
                miny = Mathf.Min(miny, _corners[i].y);
                maxx = Mathf.Max(maxx, _corners[i].x);
                maxy = Mathf.Max(maxy, _corners[i].y);
            }

            //Debug.Log($"CCC {minx}    {miny}    {maxx}    {maxy}   {_rectTransform.position}");

            _minX = screenRect.x + (_rectTransform.position.x - minx);   //计算的是向左的长度
            _maxX = screenRect.width - (maxx - _rectTransform.position.x); //计算的是向右的长度

            _minY = screenRect.y + (_rectTransform.position.y - miny);   //计算的是向下的长度
            _maxY = screenRect.height - (maxy - _rectTransform.position.y); //计算的是向上的长度

        }

        canMove = true;

        DragStart?.Invoke();

    }

    public void OnDrag(PointerEventData eventData)
    {
        if (!canMove)
            return;

        _currentPosition = _startPosition + eventData.delta;

        _currentPosition.x = Mathf.Clamp(_currentPosition.x,  _minX, _maxX);
        _currentPosition.y = Mathf.Clamp(_currentPosition.y,  _minY, _maxY);

        _startPosition = _currentPosition;
        _rectTransform.position = _currentPosition;

    }

    public void OnEndDrag(PointerEventData eventData)
    {
        canMove = false;
        DragEnd?.Invoke();
    }
}

/*
 public class MoveHandle : MonoBehaviour,IBeginDragHandler,IDragHandler
{
    private Vector3 _startPointerPosition;
    private Vector3 _startPosition;
    private Vector3 _currentPosition;

    private RectTransform _rectTransform;
    private Canvas _canvas;

    private float _scaleFactor;

    private Vector3[] _corners = new Vector3[4];



    private void Awake()
    {
        _rectTransform = GetComponent<RectTransform>();
        _canvas = _rectTransform.GetComponentInParent<Canvas>();
        _scaleFactor = _canvas.scaleFactor;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        _startPointerPosition = Input.mousePosition;
        _startPosition = transform.localPosition;
    }

    public void OnDrag(PointerEventData eventData)
    {
        _currentPosition = _startPosition + (Input.mousePosition - _startPointerPosition)/_scaleFactor;

        //限制移动范围
        {
             _rectTransform.GetWorldCorners(_corners);
            float minx=0;
            float miny=0;
            float maxx=0;
            float maxy=0;

            for (int i = 0; i < _corners.Length; i++)
            {
                if (i==0)
                {
                    minx = _corners[i].x;
                    miny = _corners[i].y;
                    maxx = _corners[i].x;
                    maxy = _corners[i].y;
                }
                else
                {
                    minx = Mathf.Min(minx,_corners[i].x);
                    miny = Mathf.Min(miny, _corners[i].y);
                    maxx = Mathf.Max(maxx, _corners[i].x);
                    maxy = Mathf.Max(maxy, _corners[i].y);
                }
            }



        }

        transform.localPosition = _currentPosition;
    }
}
 */

